package me.snowiest.snowdrift.os.cos.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.TreeMap;

@Configuration
@EnableConfigurationProperties(CosConfigProperties.class)
public class CosConfiguration {

    @Autowired
    private CosConfigProperties cosConfig;

    @Bean
    public TreeMap<String, Object> credentialConfig() {

        TreeMap<String, Object> config = new TreeMap<String, Object>();

        config.put("secretId", cosConfig.getSecretId());
        config.put("secretKey", cosConfig.getSecretKey());

        // 设置域名
        //config.put("host", "sts.internal.tencentcloudapi.com");

        // 临时密钥有效时长，单位是秒
        config.put("durationSeconds", 1800);

        // 换成你的 bucket
        config.put("bucket", cosConfig.getBuckName());
        // 换成 bucket 所在地区
        config.put("region", cosConfig.getRegion());

        // 可以通过 allowPrefixes 指定前缀数组, 例子： a.jpg 或者 a/* 或者 * (使用通配符*存在重大安全风险, 请谨慎评估使用)
        config.put("allowPrefixes", new String[]{
                "*"
        });

        // 密钥的权限列表。简单上传和分片需要以下的权限，其他权限列表请看 https://cloud.tencent.com/document/product/436/31923
        String[] allowActions = new String[]{
                // 简单上传
                "name/cos:PutObject",
                "name/cos:PostObject",
                // 分片上传
                "name/cos:InitiateMultipartUpload",
                "name/cos:ListMultipartUploads",
                "name/cos:ListParts",
                "name/cos:UploadPart",
                "name/cos:CompleteMultipartUpload"
        };
        config.put("allowActions", allowActions);

        return config;
    }
}
